package pri.damai.xiaowu.anydoor.client;

import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;
import pri.damai.xiaowu.anydoor.business.group.*;
import pri.damai.xiaowu.anydoor.business.groupchat.GroupSendReq;
import pri.damai.xiaowu.anydoor.business.groupchat.GroupSendRev;
import pri.damai.xiaowu.anydoor.business.login.LoginReqModel;
import pri.damai.xiaowu.anydoor.business.login.LoginResModel;

import java.util.Scanner;

/**
 * 客户端业务处理器
 *
 * @description
 * @author YangXinFu
 * @date 10:05 2022/7/25
 **/
public class ClientChatHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //在链接就绪时登录
        login(ctx.channel());
    }

    //登录方法
    private void login(Channel channel) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(">>用户ID：");
        Integer userid = scanner.nextInt();
        System.out.println(">>用户名称：");
        String username = scanner.next();

        LoginReqModel bean = new LoginReqModel();
        bean.setUserid(userid);
        bean.setUsername(username);
        channel.writeAndFlush(bean);
    }

    //主要是“接受服务端”的响应信息
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof LoginResModel) {
            LoginResModel res = (LoginResModel) msg;
            System.out.println("登录响应：" + res.getMsg());
            if (res.getStatus() == 0) {
                //登录成功

                //1.给通道绑定身份
                ctx.channel().attr(AttributeKey.valueOf("userid")).set(res.getUserId());

                //2.显示操作类型【请看下面】
                deal(ctx.channel());
            } else {
                //登录失败，继续登录
                login(ctx.channel());
            }
        } else if (msg instanceof CreateGroupRsp) {
            CreateGroupRsp res = (CreateGroupRsp) msg;
            System.out.println("创建响应群组：" + res);
        } else if (msg instanceof GroupListRsp) {
            GroupListRsp res = (GroupListRsp) msg;
            System.out.println("查看群组列表：" + res.getGroups());
        } else if (msg instanceof GroupAddRsp) {
            GroupAddRsp res = (GroupAddRsp) msg;
            System.out.println("加入群组响应：" + res.getMsg());
        }
//
//        }else if(msg instanceof GroupQuitResBean){
//            GroupQuitResBean res=(GroupQuitResBean)msg;
//            System.out.println("退群群组响应："+res.getMsg());
//
//        }else if(msg instanceof GroupMemberResBean){
//            GroupMemberResBean res=(GroupMemberResBean)msg;
//            if(res.getCode()==1){
//                System.out.println("查看成员列表："+res.getMsg());
//            }else{
//                System.out.println("查看成员列表："+res.getLists());
//            }
//
//        }else if(msg instanceof GroupSendMsgResBean){
//            GroupSendMsgResBean res=(GroupSendMsgResBean)msg;
//            System.out.println("群发消息响应："+res.getMsg());
//
        else if(msg instanceof GroupSendRev){
            GroupSendRev res=(GroupSendRev)msg;
            System.out.println("收到群组消息:"+ JSON.toJSONString(res));
        }
    }

    private void deal(final Channel channel) {
        final Scanner scanner = new Scanner(System.in);
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    System.out.println("请选择类型：0创建群组，1查看群组，2加入群组，3退出群组，4查看群成员，5群发消息");
                    int type = scanner.nextInt();
                    switch (type) {
                        case 0:
                            createGroup(scanner, channel);
                            break;
                        case 1:
                            listGroup(scanner, channel);
                            break;
                        case 2:
                            addGroup(scanner, channel);
                            break;
//                        case 3:
//                            quitGroup(scanner,channel);
//                            break;
//                        case 4:
//                            listMembers(scanner,channel);
//                            break;
                        case 5:
                            sendMsgToGroup(scanner,channel);
                            break;
                        default:
                            System.out.println("输入的类型不存在!");
                    }
                }
            }
        }).start();
    }

    private void createGroup(Scanner scanner, Channel channel) {

        System.out.println("请输入群组名称:");
        String groupName = scanner.next();

        CreateGroupReq createGroupReq = new CreateGroupReq();
        createGroupReq.setGroupName(groupName);

        channel.writeAndFlush(createGroupReq);
    }

    private void listGroup(Scanner scanner, Channel channel) {
        GroupListReq req = new GroupListReq();
        channel.writeAndFlush(req);
    }

    private void addGroup(Scanner scanner, Channel channel) {

        System.out.println("请输入群组名称:");
        String groupId = scanner.next();

        GroupAddReq req = new GroupAddReq();
        req.setGroupId(groupId);
        channel.writeAndFlush(req);
    }

    private void sendMsgToGroup(Scanner scanner, Channel channel) {

        System.out.println("请输入群组Id:");
        String groupId = scanner.next();
        System.out.println("请输入消息:");
        String content = scanner.next();

        GroupSendReq req = new GroupSendReq();
        req.setGroupId(groupId);
        req.setContent(content);
        channel.writeAndFlush(req);
    }
}
